package sz.com.postoffice.jxyzwgcase.service.impl;

import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.DailyIncomeSummaryDao;
import sz.com.postoffice.jxyzwgcase.pojo.DailyIncome;
import sz.com.postoffice.jxyzwgcase.pojo.DailyIncomeSummary;
import sz.com.postoffice.jxyzwgcase.pojo.DailyIncomeSummaryList;
import sz.com.postoffice.jxyzwgcase.pojo.MonthPlan;
import sz.com.postoffice.jxyzwgcase.service.DailyIncomeSummaryService;
import sz.com.postoffice.jxyzwgcase.service.MonthPlanService;
import sz.com.postoffice.jxyzwgcase.utils.DateStrUtil;

import java.util.List;
import javax.annotation.Resource;


/**
 * @ClassName DailyIncomeSummaryServiceImpl
 * @Deacription 每日收入汇总
 * @Author jmx
 * @Date 2020/11/12 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class DailyIncomeSummaryServiceImpl extends BaseServiceImpl<DailyIncomeSummary> implements DailyIncomeSummaryService {
    @Resource
    private DailyIncomeSummaryDao dailyIncomeSummaryDao;
    @Override
    protected BaseDao<DailyIncomeSummary> getMapper() {
        return this.dailyIncomeSummaryDao;
    }
    @Autowired
    private MonthPlanService monthPlanService;

    @Override
    public DailyIncomeSummary getDailyIncomeByUser(DailyIncomeSummary summary){
        DailyIncomeSummary dailyIncomeSummary = findByEntity(summary);
        if (dailyIncomeSummary == null) {
            dailyIncomeSummary = new DailyIncomeSummary();
        }
        return dailyIncomeSummary;
    }

    @Override
    public Integer getDailyVisitByUser(String userCode, String statDate) {
        String startTime = statDate + " 00:00:00";
        String endTime = statDate + " 23:59:59";
        return dailyIncomeSummaryDao.queryDailyVisit(userCode, startTime, endTime);
    }

    @Override
    public List<DailyIncomeSummary> queryAll(DailyIncomeSummary dailyIncomeSummary) {
        return dailyIncomeSummaryDao.queryAll(dailyIncomeSummary);
    }

    @Override
    public Integer sumDelivery(String userCode, String startTime, String endTime) {
        Integer sum = dailyIncomeSummaryDao.sumDelivery(userCode, startTime, endTime);
        if (sum == null) {
            return 0;
        }
        return sum;
    }
    @Override
    public Integer sumVisit(String userCode, String startTime, String endTime) {
        Integer sum = dailyIncomeSummaryDao.sumVisit(userCode, startTime, endTime);
        if (sum == null) {
            return 0;
        }
        return sum;
    }

    @Override
    public Integer sumExpressCount(String userCode, String startTime, String endTime) {
        Integer sum = dailyIncomeSummaryDao.sumExpressCount(userCode, startTime, endTime);
        if (sum == null) {
            return 0;
        }
        return sum;
    }

    @Override
    public Integer sumStandardMailCount(String userCode, String startTime, String endTime) {
        Integer sum = dailyIncomeSummaryDao.sumStandardMailCount(userCode, startTime, endTime);
        if (sum == null) {
            return 0;
        }
        return sum;
    }

    @Override
    public Integer sumFragmentedMailCount(String userCode, String startTime, String endTime) {
        Integer sum = dailyIncomeSummaryDao.sumFragmentedMailCount(userCode, startTime, endTime);
        if (sum == null) {
            return 0;
        }
        return sum;
    }

    @Override
    public Double sumExpressIncome(String userCode, String startTime, String endTime) {
        Double sum = dailyIncomeSummaryDao.sumExpressIncome(userCode, startTime, endTime);
        if (sum == null) {
            return 0.0;
        }
        return sum;
    }

    @Override
    public Double sumStandardMailIncomme(String userCode, String startTime, String endTime) {
        Double sum = dailyIncomeSummaryDao.sumStandardMailIncomme(userCode, startTime, endTime);
        if (sum == null) {
            return 0.0;
        }
        return sum;
    }

    @Override
    public Double sumFragmentedMailIncome(String userCode, String startTime, String endTime) {
        Double sum = dailyIncomeSummaryDao.sumFragmentedMailIncome(userCode, startTime, endTime);
        if (sum == null) {
            return 0.0;
        }
        return sum;
    }

    @Override
    public boolean insertSummaryByDaily(DailyIncome dailyIncome) {
        DailyIncomeSummary dailyIncomeSummary = produceIncomeSummary(dailyIncome);
        return insert(dailyIncomeSummary);
    }

    @Override
    public boolean updateIncomeFromApp(DailyIncome dailyIncome) {
        //把快递员填写的本日详情同步到汇总字段上
        DailyIncomeSummary finalSummary = new DailyIncomeSummary(dailyIncome);
        //获取本月计划
        finalSummary.setMonthPlanedIncome(getMonthPlan(dailyIncome));
        //计算本月揽收快递汇总数据
        finalSummary.setExpressCompletedCount(sumExpressCount(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getExpressCount());
        finalSummary.setExpressCompletedIncome(sumExpressIncome(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getExpressIncome());
        //计算本月走访
        finalSummary.setMonthlyVisit(sumVisit(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getDailyVisit());
        return update(finalSummary);
    }

    @Override
    public boolean updateTodayIncome(DailyIncome dailyIncome) {
        DailyIncomeSummary dailyIncomeSummary = produceIncomeSummary(dailyIncome);
        return update(dailyIncomeSummary);
    }
    @Override
    public boolean updateHistoryIncome(DailyIncome dailyIncome){
        return updateIncomeSummary(dailyIncome);
    }

    @Override
    public TableDataInfo queryListPeriod(DailyIncomeSummaryList dailyIncomeSummaryList) {
        List<DailyIncomeSummary> list = null;
        try {
            Page<?> page = startPage();
            list = dailyIncomeSummaryDao.findByDeptAndDate(dailyIncomeSummaryList.getDeptCode(),
                    dailyIncomeSummaryList.getStartDate(), dailyIncomeSummaryList.getEndDate(), dailyIncomeSummaryList.getProvinceCode(),
                    dailyIncomeSummaryList.getCityCode(), dailyIncomeSummaryList.getCountyCode());

        } catch (Exception e) {
            //logger.error("查询"+getClassName(entity)+"失败!原因是:",e);
        }
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @Override
    public List<DailyIncomeSummary> getByUserAndDate(String userCode, String startDate, String endDate) {
        return dailyIncomeSummaryDao.findByUserAndDate(userCode, startDate, endDate);
    }

    private DailyIncomeSummary produceIncomeSummary(DailyIncome dailyIncome) {
        //把快递员填写的本日详情同步到汇总字段上
        DailyIncomeSummary finalSummary = new DailyIncomeSummary(dailyIncome);
        //获取本月计划
        finalSummary.setMonthPlanedIncome(getMonthPlan(dailyIncome));
        //计算本日汇总数据
        setSumNum(finalSummary, dailyIncome);
        return finalSummary;
    }

    private Double getMonthPlan(DailyIncome dailyIncome) {
        MonthPlan tMonthPlan = new MonthPlan();
        tMonthPlan.setUserCode(dailyIncome.getUserCode());
        String strDate = dailyIncome.getStatDate().trim();
        tMonthPlan.setPlanMonth(strDate.substring(0, strDate.lastIndexOf("-")));
        MonthPlan monthPlan = monthPlanService.findByEntity(tMonthPlan);
        if (monthPlan != null) {
            return monthPlan.getPlanAmount();
        } else {
            return 0.0;
        }
    }

    private boolean updateIncomeSummary(DailyIncome dailyIncome) {
        //把需要修改的同步到汇总字段上
        DailyIncomeSummary finalSummary = new DailyIncomeSummary(dailyIncome);
        //计算当天数据的值
        setSumNum(finalSummary, dailyIncome);
        update(finalSummary);
        if (DateStrUtil.isLastDay(dailyIncome.getStatDate())) {
            return true;
        }
        //获取当前天数，查出该快递员之后的当月每一天汇总数据
        String nextDate = DateStrUtil.getNextDate(dailyIncome.getStatDate());
        String lastDate = DateStrUtil.getLastDate(dailyIncome.getStatDate());
        List<DailyIncomeSummary> modifyList = getByUserAndDate(dailyIncome.getUserCode(), nextDate, lastDate);
        if (modifyList == null) {
            return true;
        }
        //遍历修改当月每天数据
        for (DailyIncomeSummary modifySummary : modifyList) {
            DailyIncome sumData = new DailyIncome(modifySummary);
            setSumNum(modifySummary, sumData);
            update(modifySummary);
        }
        return true;
    }

    private void setSumNum(DailyIncomeSummary dailyIncomeSummary, DailyIncome dailyIncome) {
        if (DateStrUtil.isFirstDay(dailyIncome.getStatDate())) {
            dailyIncomeSummary.setStandardMailCompletedCount(dailyIncome.getStandardMailCount());
            dailyIncomeSummary.setStandardMailCompletedIncome(dailyIncome.getStandardMailIncome());
            dailyIncomeSummary.setFragmentedMailCompletedCount(dailyIncome.getFragmentedMailCount());
            dailyIncomeSummary.setFragmentedMailCompletedIncome(dailyIncome.getFragmentedMailIncome());
            dailyIncomeSummary.setExpressCompletedCount(dailyIncome.getExpressCount());
            dailyIncomeSummary.setExpressCompletedIncome(dailyIncome.getExpressIncome());
            dailyIncomeSummary.setMonthlyVisit(dailyIncome.getDailyVisit());
        } else {
            dailyIncomeSummary.setMonthlyDelivery(sumDelivery(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getDailyDelivery());
            dailyIncomeSummary.setMonthlyVisit(sumVisit(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getDailyVisit());
            dailyIncomeSummary.setExpressCompletedCount(sumExpressCount(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getExpressCount());
            dailyIncomeSummary.setExpressCompletedIncome(sumExpressIncome(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getExpressIncome());
            dailyIncomeSummary.setFragmentedMailCompletedCount(sumFragmentedMailCount(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getFragmentedMailCount());
            dailyIncomeSummary.setFragmentedMailCompletedIncome(sumFragmentedMailIncome(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getFragmentedMailIncome());
            dailyIncomeSummary.setStandardMailCompletedIncome(sumStandardMailIncomme(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getStandardMailIncome());
            dailyIncomeSummary.setStandardMailCompletedCount(sumStandardMailCount(dailyIncome.getUserCode(), DateStrUtil.getFirstDayOfMonth(dailyIncome.getStatDate()),
                    DateStrUtil.getYesterday(dailyIncome.getStatDate())) + dailyIncome.getStandardMailCount());
        }
    }



}
